package pers.cz.controller;

import pers.cz.encrypt.DesUtil;
import pers.cz.encrypt.Md5Util;
import pers.cz.encrypt.PwdHelper;
import pers.cz.pub.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: PwdHelperController
 * @author: Cheng Zhi
 * @create: 2022-03-10 17:29
 **/
@RestController
@Api(tags = "密码工具")
@RequestMapping(path = "/api/pwdHelperController")
public class PwdHelperController {

    @ApiOperation(value = "加密")
    @PostMapping(path = "/ecrypt")
    public String ecrypt(@RequestBody Map map) {

        String password = (String) map.get("password");
        String type = (String) map.get("type");
        String ecrypt = "";
        if (type.equals("easyFrame")) {
            ecrypt = PwdHelper.ecrypt(password);
        }

        if (type.equals("md5")) {
            ecrypt = Md5Util.Md5(password);
        }

        if (type.equals("des")) {
            ecrypt = DesUtil.encrypt(password);
        }

        return ecrypt;
    }

    @ApiOperation(value = "解密")
    @PostMapping(path = "/decrypt")
    public RespBean decrypt(@RequestBody Map map) {
        String password = (String) map.get("password");
        String type = (String) map.get("type");
        String decrypt = "";

        try {

            if (type.equals("easyFrame")) {
                decrypt = PwdHelper.decrypt(password);
            }

            if (type.equals("md5")) {
                return RespBean.error("MD5暂不支持解密！");
            }

            if (type.equals("des")) {
                decrypt = DesUtil.decode(password);
            }
        } catch (Exception e) {
            return RespBean.error("解密失败！请确认密码类型");
        }
        return RespBean.ok("解密成功！",decrypt);
    }
}
